<?php

namespace plugin\ky_black\app\admin\controller;

use support\Request;
use support\Response;
use plugin\ky_black\app\admin\model\ServiceRecord;

class ServiceRecordController
{
    /**
     * 获取服务记录列表
     */
    public function list(Request $request)
    {
        $query = ServiceRecord::query();
        
        // 关键词搜索
        if ($keyword = $request->input('keyword')) {
            $query->where('service_no', 'like', "%{$keyword}%")
                  ->orWhere('member_name', 'like', "%{$keyword}%");
        }
        
        // 服务类型筛选
        if ($type = $request->input('service_type')) {
            $query->where('service_type', $type);
        }
        
        // 状态筛选
        if ($status = $request->input('status')) {
            $query->where('status', $status);
        }
        
        // 日期范围筛选
        if ($startDate = $request->input('start_date')) {
            $query->where('service_time', '>=', $startDate);
        }
        if ($endDate = $request->input('end_date')) {
            $query->where('service_time', '<=', $endDate);
        }
        
        $list = $query->orderBy('created_at', 'desc')
            ->paginate($request->input('limit', 15));
            
        return json_success('获取成功', $list);
    }

    /**
     * 获取服务记录详情
     */
    public function detail(Request $request, $id)
    {
        $record = ServiceRecord::find($id);
        if (!$record) {
            return json_error('记录不存在');
        }
        
        return json_success('获取成功', $record);
    }

    /**
     * 添加服务记录
     */
    public function add(Request $request)
    {
        $data = $request->post();
        
        $record = new ServiceRecord();
        $record->service_id = $data['service_id'];
        $record->member_id = $data['member_id'];
        $record->service_type = $data['service_type'];
        $record->service_time = $data['service_time'];
        $record->description = $data['description'] ?? '';
        $record->remark = $data['remark'] ?? '';
        $record->status = 'pending';
        
        if ($record->save()) {
            return json_success('添加成功');
        }
        return json_error('添加失败');
    }

    /**
     * 编辑服务记录
     */
    public function edit(Request $request, $id)
    {
        $data = $request->post();
        
        $record = ServiceRecord::find($id);
        if (!$record) {
            return json_error('记录不存在');
        }
        
        $record->service_id = $data['service_id'];
        $record->member_id = $data['member_id'];
        $record->service_type = $data['service_type'];
        $record->service_time = $data['service_time'];
        $record->description = $data['description'] ?? $record->description;
        $record->remark = $data['remark'] ?? $record->remark;
        
        if ($record->save()) {
            return json_success('编辑成功');
        }
        return json_error('编辑失败');
    }

    /**
     * 完成服务
     */
    public function complete(Request $request)
    {
        $id = $request->input('id');
        
        $record = ServiceRecord::find($id);
        if (!$record) {
            return json_error('记录不存在');
        }
        
        $record->status = 'completed';
        $record->completed_at = date('Y-m-d H:i:s');
        
        if ($record->save()) {
            return json_success('操作成功');
        }
        return json_error('操作失败');
    }
} 